﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NationalD3.Domain.Entities;

namespace NationalD3.Infrastructure.Data.Mappings
{
    /// <summary>
    /// Student实体映射配置类
    /// </summary>
    public class StudentMap : IEntityTypeConfiguration<Student>
    {
        public void Configure(EntityTypeBuilder<Student> builder)
        {
            //设置表名
            builder.ToTable("Student").HasKey(c=>c.Id);

            builder.Property(c => c.Id)
               .HasColumnName("Id");

            builder.Property(c => c.Name)
                .HasColumnType("varchar(100)")
                .HasMaxLength(100)
                .IsRequired();

            builder.Property(c => c.Email)
                .HasColumnType("varchar(100)")
                .HasMaxLength(100)
                .IsRequired();

            builder.Property(c => c.CreatedTime)
                .HasColumnType("datetime")
                .IsRequired();

            builder.Property(c => c.BirthDate)
                .HasColumnType("date")
                .IsRequired();

            //处理值对象配置，否则会被视为实体
            //builder.OwnsOne(p => p.Address);

            //可以对值对象进行数据库重命名，还有其他的一些操作，请参考官网
            builder.OwnsOne(
                o => o.Address,
                sa =>
                {
                    sa.Property(p => p.Province).HasColumnName("Province").HasColumnType("varchar(50)").HasMaxLength(50);
                    sa.Property(p => p.City).HasColumnName("City").HasColumnType("varchar(50)").HasMaxLength(50);
                    sa.Property(p => p.County).HasColumnName("County").HasColumnType("varchar(50)").HasMaxLength(50);
                    sa.Property(p => p.Street).HasColumnName("Street").HasColumnType("varchar(200)").HasMaxLength(200);
                }
            );


            //注意：这是EF版本的写法，Core中不能使用！！！
            //builder.Property(c => c.Address.City)
            //     .HasColumnName("City")
            //     .HasMaxLength(20);
            //builder.Property(c => c.Address.Street)
            //     .HasColumnName("Street")
            //     .HasMaxLength(20);


            //如果想忽略当前值对象，可直接 Ignore
            //builder.Ignore(c => c.Address);
        }
    }
}
